package com.bestcem.xm.member.controller.v2;

import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.component.security.dto.TokenDTO;
import com.bestcem.xm.member.constant.Constants;
import com.bestcem.xm.member.controller.vo.ListFootprintsVO;
import com.bestcem.xm.member.grpc.service.param.PageParam;
import com.bestcem.xm.member.service.FootprintService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 足迹点控制器
 * @date 2021-08-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/member/v2")
public class FootprintV2Controller {
    /**
     * 足迹点服务
     */
    @Autowired
    private FootprintService footprintService;

    /**
     * 获取足迹点列表
     *
     * @param memberId
     * @return
     */
    @GetMapping("{member_id}/footprints")
    public XmResultJson<ListFootprintsVO> listFootprints(
            @PathVariable(name = "member_id", required = false) String memberId,
            @RequestParam Integer page,
            @RequestParam("rowsPerPage") Integer size,
            HttpServletRequest request) {
        // 获取企业id
        
        String orgId = SecurityContextHolder.getOrgId();
        
        String userId = SecurityContextHolder.getUserId();
        
        TokenDTO token = SecurityContextHolder.getToken();
        if (StringUtils.isBlank(orgId)) {
            log.error("【联系人】企业标识格式错误，orgId:{}", orgId);
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "获取足迹点列表失败");
        }
        if (!ObjectId.isValid(memberId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "联系人标识参数格式错误");
        }

        PageParam pageParam = new PageParam(Optional.ofNullable(page).orElse(1) - 1, Optional.ofNullable(size).orElse(10));
        if (pageParam.getPage() < 0) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE.getCode(), "当前页不能小于1");
        }
        if (pageParam.getSize() < Constants.MIN_PAGE_SIZE) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE.getCode(), "分页大小不能小于1");
        }
        if (pageParam.getSize() > Constants.MAX_PAGE_SIZE) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE.getCode(), "分页大小不能大于1000");
        }

        ServiceResult<ListFootprintsVO> result =
                footprintService.listFootprints(orgId, memberId, pageParam, userId, token.getSuperRole(), token.getIsSenior());
        return XmResultJson.handleServiceResult(result);
    }

}
